#ifndef APPCONFIG_H
#define APPCONFIG_H

#include <QObject>
#include <QSettings>
#include <QVariant>
#include <QByteArray>
#include <QCryptographicHash>
#include <QFont>
#include "module_global.h"
#include "GlobalDefine.h"
#include "screenshot/ScreenshotConfig.h"

// 权限等级枚举
enum class PermissionLevel {
    Guest = 0,    // 游客权限，通常只允许查看或基本操作
    User = 1,     // 普通用户权限，可以进行一般操作
    Admin = 2,    // 管理员权限，拥有系统管理和高级操作权限
    SuperAdmin = 3 // 超级管理员权限，拥有最高权限，可以进行所有操作，包括设置和修改系统级别的内容
};

// 用户信息结构体
struct UserInfo {
    QString macAddress;      // 电脑 MAC 地址
    PermissionLevel permissionLevel;     // 权限等级
    int activationDuration;  // 激活时长 (以小时为单位)
    QString serialNumber;    // SN 号 (序列号)

    // 构造函数，初始化默认值
    UserInfo(const QString &mac = "", PermissionLevel level = PermissionLevel::Guest, int duration = 0, const QString &sn = "")
        : macAddress(mac), permissionLevel(level), activationDuration(duration), serialNumber(sn) {}

    // 判断用户是否过期（示例：假设激活时长超过一定值即视为过期）
    bool isExpired(int maxDuration) const {
        return activationDuration > maxDuration;
    }
};

class MODULE_EXPORT AppConfig : public QObject
{
    Q_OBJECT
public:
    // 获取单例实例
    static AppConfig& getInstance();
public:
    void UpdatData();
    // 加载配置数据（从文件或默认值）
    bool loadConfig();
    // 保存配置数据到文件
    bool saveConfig() const;

    // 设置是否启用加密
    void setEncryptionEnabled(bool enabled);
public:
    // 数据访问与修改接口
    QFont getFont() const { return _font; }
    void setFont(const QFont& font) { _font = font; }

    TimerConfig getTimer() const { return _timerConfig;};
    void setTimer(const TimerConfig& timer) { _timerConfig = timer;};

    void setBackground(const BackgroundSettings& bgSetting) { _bgSettings = bgSetting;};
    BackgroundSettings getBackground() const { return _bgSettings;};

    SoundSettings getSound() const { return _soundSettings;};
    void setSound(const SoundSettings& soundSettings) {_soundSettings = soundSettings;};

    // Global sound controls
    bool getGlobalMuted() const { return _soundMuted; }
    void setGlobalMuted(bool m) { _soundMuted = m; }
    int getGlobalVolume() const { return _soundVolume; }
    void setGlobalVolume(int v) { _soundVolume = qBound(0, v, 100); }
    bool getTickEnable() const { return _tickEnable; }
    void setTickEnable(bool en) { _tickEnable = en; }
    int getTickIntervalMs() const { return _tickIntervalMs; }
    void setTickIntervalMs(int ms) { _tickIntervalMs = ms; }

    TimeoutAction getTimeoutAction() const { return _timeoutAction; };
    void setTimeoutAction(const TimeoutAction& timeoutAction) {_timeoutAction = timeoutAction;};

    PlaybackSettings getPlayback() const { return _playbackSettings;};
    void setPlayback(const PlaybackSettings& playbackSettings){ _playbackSettings= playbackSettings;};

    UserInfo getUserInfo() const { return _userInfo;};
    void setUserInfo(const UserInfo& userInfo){ _userInfo= userInfo;};

    TimeFormat getTimeFormat() const { return _timeFormat;};
    void setTimeFormat(const TimeFormat& timeFormat){ _timeFormat= timeFormat;};

    // 截图设置
    ScreenshotConfig getScreenshotConfig() const { return _screenshotConfig; }
    void setScreenshotConfig(const ScreenshotConfig& cfg) { _screenshotConfig = cfg; }

    // ========== UI状态配置 [FDD-2025-03-015] ==========
    /**
     * @brief 获取任务面板展开状态
     */
    bool getTaskPanelExpanded() const { return _taskPanelExpanded; }
    
    /**
     * @brief 设置任务面板展开状态
     */
    void setTaskPanelExpanded(bool expanded) { _taskPanelExpanded = expanded; }

signals:
    void sigDataChange();
public slots:
    void onDataChange();
private:
    explicit AppConfig(QObject *parent = nullptr);
    // 禁用拷贝构造和赋值运算符，确保实例唯一性
    AppConfig(const AppConfig&) = delete;
    AppConfig& operator=(const AppConfig&) = delete;

    virtual ~AppConfig();

    void reateConfigFileIfNotExist();
    // 初始化设置文件路径
    void initSettings(const QByteArray& encryptionKey);
    // 封装的加载配置方法
    QVariant loadSetting(const QString& group,const QString& key, const QVariant& defaultValue) const;
    // 封装的保存配置方法
    void saveSetting(const QString& group,const QString& key, const QVariant& value) const;

    // 加载字体配置
    bool loadFontConfig();
    // 保存字体配置
    bool saveFontConfig() const;

    bool loadTimerConfig();
    bool saveTimerConfig() const;

    bool saveBackgroundSettings() const;
    bool loadBackgroundSettings();

    bool loadSettings();
    bool saveSettings() const;

    bool loadTimeoutAction();
    bool saveTimeoutAction() const;

    bool saveSoundSettings() const;
    bool loadSoundSettings();

    bool saveUserInfoSettings() const;
    bool loadUserInfoSettings();

    bool saveTimeFormatSettings() const;
    bool loadTimeFormatSettings();

    bool saveGlobalSoundSettings() const;
    bool loadGlobalSoundSettings();

    bool saveScreenshotSettings() const;
    bool loadScreenshotSettings();

    // [FDD-2025-03-015] 任务面板状态持久化
    bool saveTaskPanelSettings() const;
    bool loadTaskPanelSettings();
    // 简单的加密算法（可替换为更强的算法）
    QByteArray encrypt(const QByteArray& data) const;
    // 解密算法
    QByteArray decrypt(const QByteArray& data) const;
private:
    UserInfo _userInfo;
    QFont _font;
    TimerConfig _timerConfig;  // Store TimerConfig
    TimeoutAction _timeoutAction;  // Store TimeoutAction instance
    PlaybackSettings _playbackSettings;
    // Create a BackgroundSettings object
    BackgroundSettings _bgSettings;
    SoundSettings _soundSettings;
    TimeFormat _timeFormat;
    bool _soundMuted{false};
    int _soundVolume{100};
    bool _tickEnable{false};
    int _tickIntervalMs{1000};
    ScreenshotConfig _screenshotConfig;
    bool _taskPanelExpanded{false};  // [FDD-2025-03-015] 任务面板展开状态
private:
    QString m_strFilePath;
    // QSettings指针，避免重复创建对象
    QSettings* settings;

    QByteArray encryptionKey;  // 用于加密和解密的密钥

    bool encryptionEnabled;  // 控制是否启用加密的标志
};



#endif // APPCONFIG_H
